package uit.minito.views;

import uit.minito.timduongxebuyt.R;
import uit.minito.timduongxebuyt.StaticVariables;
import android.content.Context;
import android.content.DialogInterface;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainMenu extends LinearLayout implements OnClickListener{
	
	Button menuRouterBT;
	Button menuHistoryBT;
	Button menuSettingsBT;
	Button menuFeedbackBT;
	TextView menuRouterTV;
	TextView menuHistoryTV;
	TextView menuSettingsTV;
	TextView menuFeedbackTV;
	LinearLayout mainLayout;
	public OnMainMenuListener delegate;
	boolean isShowing;

	public void initialize(Context context){
		LayoutInflater layoutInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		layoutInflater.inflate(R.layout.menu_main, this);
		
		menuRouterBT=(Button)findViewById(R.id.menuRouterBT);
		menuHistoryBT=(Button)findViewById(R.id.menuHistoryBT);
		menuSettingsBT=(Button)findViewById(R.id.menuSettingsBT);
		menuFeedbackBT=(Button)findViewById(R.id.menuFeedbackBT);
		
		
		menuRouterTV=(TextView)findViewById(R.id.menuRouterTV);
		menuHistoryTV=(TextView)findViewById(R.id.menuHistoryTV);
		menuSettingsTV=(TextView)findViewById(R.id.menuSettingsTV);
		menuFeedbackTV=(TextView)findViewById(R.id.menuFeedbackTV);
		
		mainLayout=(LinearLayout)findViewById(R.id.mainMenuLayout);
		isShowing=true;
		doAnimation(1, 0, 0); 
		
		menuRouterBT.setOnClickListener(this);
		menuHistoryBT.setOnClickListener(this); 
		menuSettingsBT.setOnClickListener(this);  
		menuFeedbackBT.setOnClickListener(this);
		
		
	} 
	
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.menuRouterBT:  
			delegate.onTouchRouter();
			break;

		case R.id.menuHistoryBT:  
			delegate.onTouchHistory();
			break;
			  
		case R.id.menuSettingsBT:
			delegate.onTouchSettings();
			break;
			
		case R.id.menuFeedbackBT:
			delegate.onTouchFeedback();
			break;
		}
		
	}
	
	public void toggleButtons(int inviOrNot){
		menuRouterBT.setVisibility(inviOrNot);
		menuHistoryBT.setVisibility(inviOrNot);
		menuSettingsBT.setVisibility(inviOrNot);
		menuFeedbackBT.setVisibility(inviOrNot);
	}
	
	public void showOrHide(){
		if(isShowing){
			hide();
		}else{
			show();
		}
	}

	public void doAnimation(float _from,float _to, long _duration){  
		AlphaAnimation alphaAnim=new AlphaAnimation(_from, _to);
		alphaAnim.setFillAfter(true);
		alphaAnim.setDuration(_duration);//(StaticVariables.SLIDE_DURATION);
		alphaAnim.setAnimationListener(new AnimationListener() {
			
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub
				
			}  
			
			public void onAnimationEnd(Animation animation) {
				// TODO Auto-generated method stub
			
				isShowing=!isShowing;
				if(isShowing==false){
					toggleButtons(INVISIBLE);
				}
				
			}
		});
		mainLayout.startAnimation(alphaAnim);
	}
	public void show(){
		if(isShowing==false){
			toggleButtons(VISIBLE);
			doAnimation(0, 1, StaticVariables.SLIDE_DURATION);
		}
		
	}
	public void hide(){
		if(isShowing==true){
			doAnimation(1, 0, StaticVariables.SLIDE_DURATION);
		}
	}
	
	public MainMenu(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		initialize(context);
	}

	public MainMenu(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		initialize(context);
	}


	

}
